package com.zf.shopping.controller;


import com.zf.shopping.bean.Result;
import com.zf.shopping.model.Commodity;
import com.zf.shopping.service.CommodityService;
import com.zf.shopping.vo.CommodityV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品
 */
@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;


    /**
     * 添加或编辑商品
     * @param commodityV
     * @return
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Result addOrUpdate(@RequestBody CommodityV commodityV) {

        return commodityService.addOrUpdate(commodityV);
    }

    /**
     * 通过commodity id来删除commodity id
     * @param commodityId
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    @ResponseBody
    public Result deleteById(@RequestParam("commodityId") Long commodityId) {

        return commodityService.deleteById(commodityId);
    }

    /**
     * 获取单个商品
     * @param commodityId
     * @return
     */
    @RequestMapping(value = "/getOneById", method = RequestMethod.GET)
    @ResponseBody
    public CommodityV getOneById(@RequestParam("commodityId") Long commodityId) {

        return commodityService.getOneById(commodityId);
    }

    /**
     * 获取所有商品
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public List<CommodityV> getAll() {

        return commodityService.getAll();
    }
}
